/*
@title: Mastermind
@description: Mastermind is a retro-themed puzzle game where players must deduce the correct sequence of colors by interpreting small hints provided after each guess. The objective is to identify the pattern using strategic color and position selections.
@author: Leonard (Omay)
@tags: ['puzzle','retro']
@addedOn: 2022-11-14

W and S for color selection, A and D for position selection
I to place
K to confirm
Try to find the correct sequence of colors.
*/
let answer = "";
//Sprites START
const red = "a";
const green = "b";
const blue = "c";
const yellow = "d";
const cyan = "e";
const pink = "f";
const empty = "g";
const guesses = "hijklmnop.qrs..tu...v".split('');
const bg = "w";
const selws = "x";
const selad = "y";
const hidden = "z";
setLegend(
  [hidden, bitmap`
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111`],
  [bg, bitmap`
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111`],
  [red, bitmap`
................
.....333333.....
...3333333333...
..333333333333..
..333333333333..
.33333333333333.
.33333333333333.
.33333333333333.
.33333333333333.
.33333333333333.
.33333333333333.
..333333333333..
..333333333333..
...3333333333...
.....333333.....
................`],
  [green, bitmap`
................
.....444444.....
...4444444444...
..444444444444..
..444444444444..
.44444444444444.
.44444444444444.
.44444444444444.
.44444444444444.
.44444444444444.
.44444444444444.
..444444444444..
..444444444444..
...4444444444...
.....444444.....
................`],
  [blue, bitmap`
................
.....555555.....
...5555555555...
..555555555555..
..555555555555..
.55555555555555.
.55555555555555.
.55555555555555.
.55555555555555.
.55555555555555.
.55555555555555.
..555555555555..
..555555555555..
...5555555555...
.....555555.....
................`],
  [yellow, bitmap`
................
.....666666.....
...6666666666...
..666666666666..
..666666666666..
.66666666666666.
.66666666666666.
.66666666666666.
.66666666666666.
.66666666666666.
.66666666666666.
..666666666666..
..666666666666..
...6666666666...
.....666666.....
................`],
  [cyan, bitmap`
................
.....777777.....
...7777777777...
..777777777777..
..777777777777..
.77777777777777.
.77777777777777.
.77777777777777.
.77777777777777.
.77777777777777.
.77777777777777.
..777777777777..
..777777777777..
...7777777777...
.....777777.....
................`],
  [pink, bitmap`
................
.....888888.....
...8888888888...
..888888888888..
..888888888888..
.88888888888888.
.88888888888888.
.88888888888888.
.88888888888888.
.88888888888888.
.88888888888888.
..888888888888..
..888888888888..
...8888888888...
.....888888.....
................`],
  [empty, bitmap`
................
.....LLLLLL.....
...LLLLLLLLLL...
..LLLLLLLLLLLL..
..LLLLLLLLLLLL..
.LLLLLLLLLLLLLL.
.LLLLLLLLLLLLLL.
.LLLLLLLLLLLLLL.
.LLLLLLLLLLLLLL.
.LLLLLLLLLLLLLL.
.LLLLLLLLLLLLLL.
..LLLLLLLLLLLL..
..LLLLLLLLLLLL..
...LLLLLLLLLL...
.....LLLLLL.....
................`],
  [guesses[1], bitmap`
................
..3333....LLLL..
.333333..LLLLLL.
.333333..LLLLLL.
.333333..LLLLLL.
.333333..LLLLLL.
..3333....LLLL..
................
................
..LLLL....LLLL..
.LLLLLL..LLLLLL.
.LLLLLL..LLLLLL.
.LLLLLL..LLLLLL.
.LLLLLL..LLLLLL.
..LLLL....LLLL..
................`],
  [guesses[2], bitmap`
................
..3333....3333..
.333333..333333.
.333333..333333.
.333333..333333.
.333333..333333.
..3333....3333..
................
................
..LLLL....LLLL..
.LLLLLL..LLLLLL.
.LLLLLL..LLLLLL.
.LLLLLL..LLLLLL.
.LLLLLL..LLLLLL.
..LLLL....LLLL..
................`],
  [guesses[3], bitmap`
................
..3333....3333..
.333333..333333.
.333333..333333.
.333333..333333.
.333333..333333.
..3333....3333..
................
................
..3333....LLLL..
.333333..LLLLLL.
.333333..LLLLLL.
.333333..LLLLLL.
.333333..LLLLLL.
..3333....LLLL..
................`],
  [guesses[4], bitmap`
................
..3333....3333..
.333333..333333.
.333333..333333.
.333333..333333.
.333333..333333.
..3333....3333..
................
................
..3333....3333..
.333333..333333.
.333333..333333.
.333333..333333.
.333333..333333.
..3333....3333..
................`],
  [guesses[5], bitmap`
................
..2222....LLLL..
.222222..LLLLLL.
.222222..LLLLLL.
.222222..LLLLLL.
.222222..LLLLLL.
..2222....LLLL..
................
................
..LLLL....LLLL..
.LLLLLL..LLLLLL.
.LLLLLL..LLLLLL.
.LLLLLL..LLLLLL.
.LLLLLL..LLLLLL.
..LLLL....LLLL..
................`],
  [guesses[6], bitmap`
................
..3333....2222..
.333333..222222.
.333333..222222.
.333333..222222.
.333333..222222.
..3333....2222..
................
................
..LLLL....LLLL..
.LLLLLL..LLLLLL.
.LLLLLL..LLLLLL.
.LLLLLL..LLLLLL.
.LLLLLL..LLLLLL.
..LLLL....LLLL..
................`],
  [guesses[7], bitmap`
................
..3333....3333..
.333333..333333.
.333333..333333.
.333333..333333.
.333333..333333.
..3333....3333..
................
................
..2222....LLLL..
.222222..LLLLLL.
.222222..LLLLLL.
.222222..LLLLLL.
.222222..LLLLLL.
..2222....LLLL..
................`],
  [guesses[8], bitmap`
................
..3333....3333..
.333333..333333.
.333333..333333.
.333333..333333.
.333333..333333.
..3333....3333..
................
................
..3333....2222..
.333333..222222.
.333333..222222.
.333333..222222.
.333333..222222.
..3333....2222..
................`],
  [guesses[10], bitmap`
................
..2222....2222..
.222222..222222.
.222222..222222.
.222222..222222.
.222222..222222.
..2222....2222..
................
................
..LLLL....LLLL..
.LLLLLL..LLLLLL.
.LLLLLL..LLLLLL.
.LLLLLL..LLLLLL.
.LLLLLL..LLLLLL.
..LLLL....LLLL..
................`],
  [guesses[11], bitmap`
................
..3333....2222..
.333333..222222.
.333333..222222.
.333333..222222.
.333333..222222.
..3333....2222..
................
................
..2222....LLLL..
.222222..LLLLLL.
.222222..LLLLLL.
.222222..LLLLLL.
.222222..LLLLLL.
..2222....LLLL..
................`],
  [guesses[12], bitmap`
................
..3333....3333..
.333333..333333.
.333333..333333.
.333333..333333.
.333333..333333.
..3333....3333..
................
................
..2222....2222..
.222222..222222.
.222222..222222.
.222222..222222.
.222222..222222.
..2222....2222..
................`],
  [guesses[15], bitmap`
................
..2222....2222..
.222222..222222.
.222222..222222.
.222222..222222.
.222222..222222.
..2222....2222..
................
................
..2222....LLLL..
.222222..LLLLLL.
.222222..LLLLLL.
.222222..LLLLLL.
.222222..LLLLLL.
..2222....LLLL..
................`],
  [guesses[16], bitmap`
................
..3333....2222..
.333333..222222.
.333333..222222.
.333333..222222.
.333333..222222.
..3333....2222..
................
................
..2222....2222..
.222222..222222.
.222222..222222.
.222222..222222.
.222222..222222.
..2222....2222..
................`],
  [guesses[20], bitmap`
................
..2222....2222..
.222222..222222.
.222222..222222.
.222222..222222.
.222222..222222.
..2222....2222..
................
................
..2222....2222..
.222222..222222.
.222222..222222.
.222222..222222.
.222222..222222.
..2222....2222..
................`],
  [guesses[0], bitmap`
................
..LLLL....LLLL..
.LLLLLL..LLLLLL.
.LLLLLL..LLLLLL.
.LLLLLL..LLLLLL.
.LLLLLL..LLLLLL.
..LLLL....LLLL..
................
................
..LLLL....LLLL..
.LLLLLL..LLLLLL.
.LLLLLL..LLLLLL.
.LLLLLL..LLLLLL.
.LLLLLL..LLLLLL.
..LLLL....LLLL..
................`],
  [selws, bitmap`
2222222222222222
22222......22222
222..........222
22............22
22............22
2..............2
2..............2
2..............2
2..............2
2..............2
2..............2
22............22
22............22
222..........222
22222......22222
2222222222222222`],
  [selad, bitmap`
2222222222222222
22222......22222
222..........222
22............22
22............22
2..............2
2..............2
2..............2
2..............2
2..............2
2..............2
22............22
22............22
222..........222
22222......22222
2222222222222222`]
);
setBackground(bg);
setSolids("hijklmnopqrstuvxyz".split(''));
//Sprites END
//Maps START
let level = 0;
const levels = [
  map`
ggggha
gggghb
gggghc
gggghd
gggghe
gggghf
gggghz
ggggh.
ggggh.
ggggh.
ggggh.
ggggh.`,
];
setMap(levels[level]);
addSprite(0, 0, selad);
addSprite(5, 0, selws);
//Maps END
//Game START
for(var i = 0; i < 4; i++){
  answer += "abcdef".charAt(Math.round(Math.random()*5));
}
answer = answer.split('');
//Game END
//Controls START
let guess = [-1,-1,-1,-1];
onInput("w", () => {
  getFirst(selws).y -= 1
});
onInput("s", () => {
  getFirst(selws).y += 1
});
onInput("a", () => {
  getFirst(selad).x -= 1
});
onInput("d", () => {
  getFirst(selad).x += 1
});
onInput("i", () => {
  let toPlace = getTile(getFirst(selad).x, getFirst(selad).y);
  let toPlaceTypes = toPlace.map(x => x.type);
  let thing = "abcdef";
  for(var i = 0; i < toPlace.length; i++){
    if(thing.includes(toPlaceTypes[i])){
      toPlace[i].remove();
    }
  }
  addSprite(getFirst(selad).x, getFirst(selad).y, thing.charAt(getFirst(selws).y));
  guess[getFirst(selad).x] = thing.charAt(getFirst(selws).y);
});
onInput("k", () => {
  if(!guess.includes(-1)){
    var ansID = 0;
    for(var i = 0; i < answer.length; i++){
      if(answer[i] === guess[i]){
        ansID += 5;
        guess[i] = -1;
      }else if(guess.includes(answer[i])){
        ansID++;
      }
    }
    addSprite(4, getFirst(selad).y, guesses[ansID]);
    guess = [-1,-1,-1,-1];
    if(ansID === 20){
      addText("You Win!", {color: color`0`});
      setMap(`${answer[0]}${answer[1]}${answer[2]}${answer[3]}`);
    }else if(getFirst(selad).y === height()-1){
      addText("You Lose.", {color: color`0`, y: 1});
      addText("The answer was:", {color: color`0`, y: 3});
      setMap(`${answer[0]}${answer[1]}${answer[2]}${answer[3]}`);
    }else{
      getFirst(selad).y++;
    }
  }
});
afterInput(() => {
  
});
//Constrols END